package uniandes.dermatologia;

import java.util.ArrayList;

import uniandes.dermatologia.singleton.DermatologiaUniandesApp;
import uniandes.dermatologia.training.TestLogic;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;

public class TestListActivity extends Activity {
	
	private Spinner spTests;
	
	private TextView txtNoTests;
	
	private Button btnGoToTest;
	
	private TestLogic selectedTest;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_test_list);
		
		/*
		 * spTests = (Spinner)findViewById(R.id.spTests);
		
		txtNoTests = (TextView)findViewById(R.id.txtNoTests);
		txtNoTests.setText("No tiene pruebas pendientes");
		txtNoTests.setVisibility(View.INVISIBLE);
		
		btnGoToTest = (Button)findViewById(R.id.btnGoToTest);
		
		
		final DermatologiaUniandesApp app = (DermatologiaUniandesApp) getApplication();
		final ArrayList<TestLogic> testList = app.getTests();
		if(testList != null && testList.size() > 0)
		{
			
			ArrayList<String> tests = new ArrayList<String>();
			
			for(TestLogic t: testList)
	        {
				if(t.isAccessible())
				{
					tests.add(t.getName());
				}
	        }
			
			if(tests.size()>0)
			{
				String[] testNames = new String[tests.size()];
				for(int i = 0; i < tests.size(); i ++)
				{
					testNames[i] = tests.get(i);
				}
				ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, testNames);
				spTests.setAdapter(adapter);
				spTests.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
					
					@Override
					public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
						selectedTest = testList.get(i);
						app.setCurrentTestLogic(selectedTest);
						if(app.getCurrentCaseLogic() == null && app.getCurrentTestLogic().isWithCases())
						{
							app.setCurrentCaseLogic(selectedTest.getCaseLogics().get(0));
							app.setCurrentCaseIndex(0);
						}					
						btnGoToTest.setEnabled(true);
					}
					@Override
					public void onNothingSelected(AdapterView<?> arg0) {
						btnGoToTest.setEnabled(false);
						app.setCurrentTestLogic(null);
					}
					
				});
				
			}
			
		}
		else
		{
			spTests.setVisibility(View.GONE);
			txtNoTests.setVisibility(View.VISIBLE);
			btnGoToTest.setEnabled(false);
			
		}
		
		btnGoToTest.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(app.getCurrentTestLogic().isWithCases())
				{
					Intent intent = new Intent(TestListActivity.this, TestDetailActivity.class);
					startActivity(intent);
				}
			}
		});
		 */
		
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.test_list, menu);
		return true;
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		init();
	}
	
	public void init()
	{
		spTests = (Spinner)findViewById(R.id.spTests);
		
		txtNoTests = (TextView)findViewById(R.id.txtNoTests);
		txtNoTests.setText("No tiene pruebas pendientes");
		txtNoTests.setVisibility(View.INVISIBLE);
		
		btnGoToTest = (Button)findViewById(R.id.btnGoToTest);
		
		
		final DermatologiaUniandesApp app = (DermatologiaUniandesApp) getApplication();
		final ArrayList<TestLogic> testList = app.getTests();
		if(testList != null && testList.size() > 0)
		{
			
			ArrayList<String> tests = new ArrayList<String>();
			
			for(TestLogic t: testList)
	        {
				if(t.isAccessible())
				{
					tests.add(t.getName());
				}
	        }
			
			if(tests.size()>0)
			{
				String[] testNames = new String[tests.size()];
				for(int i = 0; i < tests.size(); i ++)
				{
					testNames[i] = tests.get(i);
				}
				ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, testNames);
				spTests.setAdapter(adapter);
				spTests.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
					
					@Override
					public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
						selectedTest = testList.get(i);
						app.setCurrentTestLogic(selectedTest);
						if(app.getCurrentCaseLogic() == null && app.getCurrentTestLogic().isWithCases())
						{
							app.setCurrentCaseLogic(selectedTest.getCaseLogics().get(0));
							app.setCurrentCaseIndex(0);
						}					
						btnGoToTest.setEnabled(true);
					}
					@Override
					public void onNothingSelected(AdapterView<?> arg0) {
						btnGoToTest.setEnabled(false);
						app.setCurrentTestLogic(null);
					}
					
				});
				
			}
			
		}
		else
		{
			spTests.setVisibility(View.GONE);
			txtNoTests.setVisibility(View.VISIBLE);
			btnGoToTest.setVisibility(View.GONE);
			btnGoToTest.setEnabled(false);
			
		}
		
		btnGoToTest.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(app.getCurrentTestLogic().isWithCases())
				{
					Intent intent = new Intent(TestListActivity.this, TestDetailActivity.class);
					startActivity(intent);
				}
			}
		});
	}
	
	
	
	

}
